﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BLL;

public partial class Login : System.Web.UI.Page
{
    private MembersBll bll = new MembersBll();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Session.Clear();

            if (Request.Cookies["UserName"] != null && Request.Cookies["UserName"].Value != null)
            {
                this.txtUserName.Text = Request.Cookies["UserName"].Value;
            }

            //if ( Request.Cookies["UserName"] != null && Request.Cookies["UserName"].Value != null &&
            //      Request.Cookies["Password"] != null && Request.Cookies["Password"].Value != null
            //    )
            //{
            //    this.txtUserName.Text = Request.Cookies["UserName"].Value;
            //    this.txtPassWord.Text = Request.Cookies["Password"].Value;

            //    this.btnLogin_Click(null, null);
            //}

        }

        //Session["MemberID"] = "1";
        //Response.Redirect("FinaceRecordManager.aspx");
    }

    protected void btnLogin_Click(object sender, EventArgs e)
    {
        string userName = this.txtUserName.Text;
        string password = this.txtPassWord.Text;
        Members user = bll.CheckLogin(userName, password);

        if (user == null)
        {
            Library.WebCommon.Alert(this, "UserName or PassWord error");
            //若出错将Cookie同时移除
            //Response.Cookies.Remove("UserName");
            //Response.Cookies.Remove("Password");

            return;
        }

        Session["MemberID"] = user.MemberID;
        Session["MemberName"] = user.MemberName;

        Response.Cookies.Add(new HttpCookie("UserName", this.txtUserName.Text));

        //两周内不过期
        //if (chkCookie.Checked)
        //{
            
        //    Response.Cookies.Add(new HttpCookie("UserName",this.txtUserName.Text));
        //    Response.Cookies.Add(new HttpCookie("Password",this.txtPassWord.Text));

        //    Response.Cookies["UserName"].Expires = DateTime.Now.AddDays(14.0);
        //    Response.Cookies["Password"].Expires = DateTime.Now.AddDays(14.0);
        //}

        Response.Redirect("FinaceRecordManager.aspx");
    }

}
